package com.liu.udpDemo;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.Scanner;

/*udp协议服务端还是要一直监听*/
public class UdpServer {
    public static void main(String[] args) {
        DatagramSocket datagramSocket =null;
        try {
//            开放端口
            datagramSocket =new DatagramSocket(9876);
//            接收数据包
            byte[] buffer = new byte[1024];
            DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
            datagramSocket.receive(packet);//接收方法会阻塞
            System.out.println(packet.getAddress());
            System.out.println(packet.getPort());
            System.out.println("请输入要保持的位置");
            FileOutputStream fos=new FileOutputStream(new String(new Scanner(System.in).next()));
            for(int i=0;i<packet.getData().length;i++){
                fos.write(packet.getData(),0,packet.getData().length);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {

        }
    }
}
